package com.yonyou.brigade.util.operateutil;

import com.yonyou.ybzboot.starter.common.exception.CustomException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by Administrator on 2018/6/19.
 */
public class DateUtils {
    private static Logger logger = LoggerFactory.getLogger(DateUtils.class);
    //DD中的日期时间格式（年-月-日 时:分:秒）
    public final static String DD_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public final static String DD_DATE_FORMAT = "yyyy-MM-dd";

    /**
     * 时间戳转为 yyyy-MM-dd HH:mm:ss 格式字符串
     * @param dateLong
     * @return
     */
    public static String longToDateTime(Long dateLong) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DD_DATETIME_FORMAT);
        String date = null;
        try {
            date = dateFormat.format(new Date(dateLong));
            return date;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 字符串转换为日期
     *
     * @param strDate 日期字符串
     * @return
     * @deprecated use {@link #str2Date(String, String)} instead
     */
    @Deprecated
    public static Date strToDate(String strDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DD_DATE_FORMAT);
        Date date = null;
        try {
            date = dateFormat.parse(strDate);
        } catch (ParseException e) {
            logger.error(e.getMessage());
        }
        return date;
    }

    /**
     * 日期转字符串
     *
     * @param date 日期
     * @return
     * @deprecated use {@link #date2Str(Date, String)} instead
     */
    @Deprecated
    public static String dateToString(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DD_DATETIME_FORMAT);
        return format.format(date);
    }

    public static String getCurrentDateMinusOneDay() {
        return DateUtils.getCurrentDateMinusOneDay(DD_DATE_FORMAT);
    }

    public static String getCurrentDateMinusOneDay(String format) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, -1);
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(cal.getTime());
    }

    public static String getCurrentDateMinusThreeDays(String format) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, -3);
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(cal.getTime());
    }

    public static String getCurrentDateMinusTwoMonths(String format) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -2);
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(cal.getTime());
    }

    public static String getCurrentDateDay() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        SimpleDateFormat dateFormat = new SimpleDateFormat(DD_DATE_FORMAT);
        return dateFormat.format(cal.getTime());
    }

    public static String getCurrentDateTimeMinusOneDay() {
        return DateUtils.getCurrentDateTimeMinusOneDay(DD_DATETIME_FORMAT);
    }

    public static String getCurrentDateTimeMinusOneDay(String format) {
        return DateUtils.getCurrentDateMinusOneDay(format);
    }

    public static String getCurrentDateTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        SimpleDateFormat dateFormat = new SimpleDateFormat(DD_DATETIME_FORMAT);
        return dateFormat.format(cal.getTime());
    }

    /**
     * @param
     */
    public static java.sql.Time strToTime(String strDate) {
        String str = strDate;
        if (str != null) {
            if (str.contains(" ")) {
                str = str.substring(str.indexOf(" ") + 1);
            }
            List<String> ls = RegexUtils.getMatchers(":", str);
            if (ls.size() == 1) {
                str = str.concat(":00");
            }
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            java.util.Date d = null;
            try {
                d = format.parse(str);
            } catch (Exception e) {
                e.printStackTrace();
            }
            java.sql.Time time = new java.sql.Time(d.getTime());
            if (str.contains(".000")) {
                str = str.substring(0,8);
            }
            return time.valueOf(str);
        } else {
            return null;
        }
    }


    /**
     * @param
     */
    public static java.sql.Time dateToTime(Date date) {
        String strDate = dateToString(date);
        return strToTime(strDate);
    }

    /**
     * @param
     * @deprecated use {@link #str2Date(String, String)} instead
     */
    @Deprecated
    public static Date strToDateTime(String strDate) {
        String str = strDate;
        List<String> ls = RegexUtils.getMatchers(":", strDate);
        if (ls.size() == 1) {
            str = strDate.concat(":00");
        }
        java.util.Date d = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat(DD_DATETIME_FORMAT);
            d = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return d;
    }

    /**
     * 指定日期加上天数后的日期
     *
     * @param num     为增加的天数
     * @param newDate 创建时间
     * @return
     * @throws ParseException
     * @deprecated use {@link #plusDay(String, int, String)} instead
     */
    @Deprecated
    public static String plusDay(int num, Date newDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = format.format(newDate);
        Date currdate = null;
        try {
            currdate = format.parse(format1);
        } catch (ParseException e) {
            logger.error("工具类DateUtils中指定日期加上天数后的日期方法中的日期格式化出现错误" + e.getMessage());
        }
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
        currdate = ca.getTime();
        String enddate = format.format(currdate);
        return enddate;
    }

    /**
     * 将世界标准时间转换为目标时区的本地时间
     *
     * @param gmtDate
     * @param id
     * @return
     */
    public static Date convertGMTToLocal(Date gmtDate, String id) {
        if (gmtDate != null) {
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            calendar.setTime(gmtDate);
            calendar.setTimeZone(TimeZone.getTimeZone(id));
            int zoneOffset = calendar.get(Calendar.ZONE_OFFSET);
            int dstOffset = calendar.get(Calendar.DST_OFFSET);
            calendar.add(Calendar.MILLISECOND, dstOffset + zoneOffset);
            return calendar.getTime();
        } else {
            return null;
        }
    }


    /**
     * 指定日期按照格式增加指定天数
     *
     * @param num        增加天数可为负
     * @param dateStr    日期字符串
     * @param dateFormat 日期格式
     * @return
     */
    public static String plusDay(String dateStr, int num, String dateFormat) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        Date date;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            throw new CustomException("日期格式转换异常");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // num为增加的天数，可以改变的
        calendar.add(Calendar.DAY_OF_MONTH, num);
        return format.format(calendar.getTime());
    }

    /**
     * 指定日期按照格式增加指定天数
     *
     * @param num  增加天数可为负
     * @param date 日期
     * @return
     */
    public static Date plusDay(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // num为增加的天数，可以改变的
        calendar.add(Calendar.DAY_OF_MONTH, num);
        return calendar.getTime();
    }


    /**
     * 按指定日期格式将字符串转为日期
     *
     * @param dateStr    日期字符串
     * @param dateFormat 日期格式
     * @return
     */
    public static Date str2Date(String dateStr, String dateFormat) {
        DateFormat df = new SimpleDateFormat(dateFormat);
        try {
            return df.parse(dateStr);
        } catch (ParseException e) {
            throw new CustomException("日期格式转换错误");
        }
    }

    /**
     * 按指定日期格式将日期转为字符串
     *
     * @param date       日期
     * @param dateFormat 日期格式
     * @return
     */
    public static String date2Str(Date date, String dateFormat) {
        DateFormat df = new SimpleDateFormat(dateFormat);
        return df.format(date);
    }
}
